package com.dhcc.core.modules.gen.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.annotion.Permission;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.constant.Const;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.core.modules.gen.entity.Table;
import com.dhcc.core.modules.gen.service.IGeneratorService;

/**
 * 代码生成控制器
 * 
 * @ClassName: GeneratorController
 * @Description: TODO
 * @author: cyf
 * @date: 2018年1月2日 上午10:14:37
 */
@Controller
@RequestMapping("${sys.backendPath}/gen")
public class GeneratorController extends BaseController {

    private String PREFIX = "/backend/gen/";

    @Autowired
    private IGeneratorService generatorService;

    /**
     * 跳转到代码生成首页
     */
    @Permission(roles = Const.ADMIN_NAME)
    @RequestMapping
    public String index() {
        return PREFIX + "gen.html";
    }

    /**
     * 获取数据库表列表
     */
    @Permission(roles = Const.ADMIN_NAME)
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam Map<String, Object> params) {
        List<Table> list = generatorService.queryList(params);
        int total = generatorService.queryTotal(params);
        Page<Table> page = new PageFactory<Table>().defaultPage();
        page.setRecords(list);
        page.setTotal(total);
        return R.packForBT(page);
    }

    /**
     * 跳转到代码生成信息页
     */
    @Permission(roles = Const.ADMIN_NAME)
    @RequestMapping(value = "/gen_info/{tableNames}")
    public String info(@PathVariable String tableNames, Model model) {
        model.addAttribute("tableNames", tableNames);
        return PREFIX + "gen_info.html";
    }

    /**
     * 代码生成
     * 
     * @throws IOException
     */
    @RequestMapping(value = "/gen", method = RequestMethod.POST)
    @ResponseBody
    @Permission(roles = Const.ADMIN_NAME)
    public void add(String tableNames, String modularName, String customName, String author, String tablePrefix,
            int usePermission, int useLog, HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        if (CommonUtil.isOneEmpty(tableNames, modularName)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }

        byte[] data = generatorService.generatorCode(modularName, customName, author, tableNames, tablePrefix,
                usePermission, useLog);

        response.reset();
        response.setHeader("Content-Disposition", "attachment; filename=\"" + modularName + "-code.zip\"");
        response.addHeader("Content-Length", "" + data.length);
        response.setContentType("application/octet-stream; charset=UTF-8");

        IOUtils.write(data, response.getOutputStream());
    }
}
